package com.ruoyi.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.lionsoul.ip2region.xdb.Searcher;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @title: IpUtil
 * @copyright: copyright (c) 2023
 * @author: jiangguanjun
 * @date: 2023/1/30 17:19
 * @firstReview:
 * @lastReview:
 * @desc:
 */

@Slf4j
public class IpUtil {
    public static String getIpAddr(HttpServletRequest request) {
        String ipAddress = null;
        try {
            ipAddress = request.getHeader("x-forwarded-for");
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getRemoteAddr();
                if (ipAddress.equals("127.0.0.1")) {
                    // 根据网卡取本机配置的IP
                    InetAddress inet = null;
                    try {
                        inet = InetAddress.getLocalHost();
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                    }
                    ipAddress = inet.getHostAddress();
                }
            }
            // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
            if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
                // = 15
                if (ipAddress.indexOf(",") > 0) {
                    ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
                }
            }
        } catch (Exception e) {
            ipAddress="";
        }
        // ipAddress = this.getRequest().getRemoteAddr();

        return ipAddress;
    }

    public static String getIpAddrs(HttpServletRequest request) {
        String ip = request.getHeader("X-Real-IP");
        if (ip != null && !"".equals(ip) && !"unknown".equalsIgnoreCase(ip)) {
            return ip;
        }
        ip = request.getHeader("X-Forwarded-For");
        if (ip != null && !"".equals(ip) && !"unknown".equalsIgnoreCase(ip)) {
            int index = ip.indexOf(',');
            if (index != -1) {
                return ip.substring(0, index);
            } else {
                return ip;
            }
        } else {
            return request.getRemoteAddr();
        }
    }

    public static String getCityDetailByIp(String ip, Searcher searcher,String split,Integer ...cityIndex){
        if(split == null){
            split="" ;
        }

        StringBuffer res = new StringBuffer("") ;
        try {
            if(cityIndex== null || cityIndex.length == 0){
                log.warn("要求取的城市维度错误：{}",cityIndex);
                return "" ;
            }

            String region = searcher.search(ip);
            log.info("region::::{}",region);
            String [] citys =  region.split("\\|") ;

            for (Integer index:cityIndex) {
                res.append(citys[index]).append(split) ;
            }

        } catch (Exception e) {
            System.out.printf("failed to search(%s): %s\n", ip, e);
        }
        if(res.length()!=0){
            if(StringUtils.isBlank(split)){
                return res.substring(0,res.length()) ;
            }
            return res.substring(0,res.length()-1) ;
        }

        return "" ;
    }
}
